// pages/account/account.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentTab: 0,
        nextTab: 0,
        activeTab: null,
        arrList: [
            "现金",
            "支付宝",
            "微信",
            "储蓄卡",
            "信用卡"
        ],
        iconList: [],
        daily: '', //日常消费
        icon: '', //花销哪里
        txt: '', //花销哪里
        payment: '', //账户选择
        date: '2022-01-01', //日期
        money: '', //金额
        commit: '', //备注
        amountList: [], //数组
        count: 0,
    },
    //点击图标，变大，其余不变
    iconFn: function (e) {
        // let query = wx.createSelectorQuery();
        // query.select('.list_icon').boundingClientRect();
        // query.exec(function(res){
        //    console.log(res);
        //    console.log(res[0].height)
        // })
        let act = e.currentTarget.dataset.current;
        let icon = e.currentTarget.dataset.icon;
        let txt = e.currentTarget.dataset.txt;
        // console.log(icon,txt);
        if (this.data.activeTab == act) {
            return false;
        } else {
            this.setData({
                activeTab: act,
                icon: icon,
                txt: txt
            })
        }
    },
    //切换逻辑
    switchTab: function (e) {
        let cur = e.currentTarget.dataset.current;
        let payment = e.currentTarget.dataset.payment;
        // console.log(payment);
        if (this.data.currentTab == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur,
                payment: payment
            })
        }
    },
    switchNaN: function (e) {
        var cur = e.currentTarget.dataset.index;
        var txt = e.currentTarget.dataset.text;
        // console.log(txt);
        if (this.data.nextTab == cur) {
            return false;
        } else {
            this.setData({
                nextTab: cur,
                daily: txt
            })
        }
    },
    //编写方法获取数据
    getIcon() {
        let that = this;
        wx.cloud.callFunction({
            name: "getIcon",
            success: function (res) {
                // console.log(res);
                if (res.errMsg == "cloud.callFunction:ok") {
                    //赋值
                    that.setData({
                        iconList: res.result.iconArray,
                    })
                }
            }
        })
    },
    //点击获取日期
    bindDateChange: function (e) {
        // console.log(e.detail.value,"输入的值");
        this.setData({
            date: e.detail.value
        })
    },
    //获取输入框的值
    getMonValue(e) {
        let that = this;

        function getMoney() {
            let money = e.detail.value;
            that.setData({
                money: money
            })
        }
        setTimeout(getMoney, 300)
    },
    getComValue(e) {
        let that = this;

        let commit = e.detail.value;
        let len = commit.length;
        if (len > 13) {
            commit = "";
        }
        // console.log(len,"commit");
        that.setData({
            commit: commit
        })

        // console.log(commit,"commit");
    },
    //获取所有的值，保存到全局上
    UpdateBatch: function () {
        //金额未选中
        if (this.data.money == "") {
            wx.showToast({
                title: "请输入金额",
                icon: 'none',
                duration: 1000,
                mask: true
            })
            this.data.amountList = [];
        }
        // 记账类型没被选中
        if (this.data.icon == "") {
            wx.showToast({
                title: "请选择记账类型",
                icon: 'none',
                duration: 1000,
                mask: true
            })
            this.data.amountList = [];
        }

        let obj = {};
        obj.daily = this.data.daily;
        obj.icon = this.data.icon;
        obj.text = this.data.txt;
        obj.payment = this.data.payment;
        obj.payment = this.data.date;
        obj.money = this.data.money;
        obj.commit = this.data.commit;
        obj.index = this.data.count;
        if (obj.daily == "" || obj.icon == "" || obj.text == "" || obj.payment == "" ||
            obj.payment == "" || obj.money == "") {
            wx.showToast({
                title: "数据不能为空",
                icon: 'none',
                duration: 1000,
                mask: true
            })
            return;
        } else {
            this.data.amountList.push(obj);
        }

        wx.cloud.callFunction({
            name : 'add_booking',
            data:{
            account : this.data.daily,
            icon : this.data.icon,
            text : this.data.txt,
            mainType : this.data.payment,
            date : this.data.date,
            money : this.data.money,
            commit : this.data.commit,
            index : this.data.count
            }
        }).then(res => {
            console.log('res==>',res);
        }).catch(err => {
            console.log('err ==> ',err);
        })
        // console.log(this.data.amountList);
        // console.log(data.detail.value); 
        if (this.data.amountList != "") {
            let app = getApp();
            app.globalData.dataList = this.data.amountList;
            console.log(app.globalData);
            wx.showToast({
                title: "保存成功",
                icon: 'none',
                duration: 1000,
                mask: true
            })
        }

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
        this.getIcon();
        //  wx.showLoading({
        //     title: '加载中',
        //  })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})